import {
  RouteLocationNormalized,
  RouteRecordRaw,
  RouteRecordNormalized,
} from 'vue-router';
// eslint-disable-next-line import/no-cycle
import { useMenuStore, useUserStore } from '@/store';
import {
  DEFAULT_ROUTE,
  STATUS_ROUTER_LIST,
  WHITE_LIST,
} from '@/router/constants';
import { AdminRoleCode } from '@/types/const';

export default function usePermission() {
  const menuStore = useMenuStore();
  const userStore = useUserStore();
  return {
    /**
     * 是否可访问路由
     */
    accessRouter(route: RouteLocationNormalized | RouteRecordRaw) {
      // 没有名字证明路由不存在
      if (route.name === undefined) {
        return false;
      }
      // 检查路由是否存在于授权路由中
      const menuConfig = [
        ...menuStore.appAsyncMenus,
        ...WHITE_LIST,
        ...STATUS_ROUTER_LIST,
        DEFAULT_ROUTE,
      ];

      let exist = false;
      while (menuConfig.length && !exist) {
        const element = menuConfig.shift();
        if (element?.name === route.name) exist = true;
        // @ts-ignore
        if (element?.children) {
          menuConfig.push(
            // @ts-ignore
            ...(element.children as unknown as RouteRecordNormalized[])
          );
        }
      }
      return exist;
    },
    /**
     * 是否有权限
     */
    hasPermission(permission: string) {
      return (
        userStore.permission?.includes('*') ||
        userStore.permission?.includes(permission)
      );
    },
    /**
     * 是否有权限
     */
    hasAnyPermission(permission: string[]) {
      return (
        userStore.permission?.includes('*') ||
        permission.map((s) => userStore.permission?.includes(s)).filter(Boolean)
          .length > 0
      );
    },
    /**
     * 是否有角色
     */
    hasRole(role: string) {
      return (
        userStore.roles?.includes(AdminRoleCode) ||
        userStore.roles?.includes(role)
      );
    },
    /**
     * 是否有角色
     */
    hasAnyRole(role: string[]) {
      return (
        userStore.roles?.includes(AdminRoleCode) ||
        role.map((s) => userStore.roles?.includes(s)).filter(Boolean).length > 0
      );
    },
  };
}
